package com.starsoft.frame.core.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

@Component
public class CustomSimpleMappingExceptionResolver extends
		SimpleMappingExceptionResolver {

	private static Logger logger = LoggerFactory.getLogger(CustomSimpleMappingExceptionResolver.class);
	
	@Override
	protected ModelAndView doResolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
		 
		logger.error("Exception Resolver", ex);
		if (request.getRequestURI().contains(".json")){
			// JSON格式返回
			MappingJackson2JsonView jsonView = new MappingJackson2JsonView();
			jsonView.setAttributesMap(WebUtil.errorModel(ex.getMessage()));
			return new ModelAndView(jsonView);
		}
		return super.doResolveException(request, response, handler, ex);
	}
}